package juc;

class Ticket{
    private static int sum=20;
    public  synchronized void saleTicket(){
        if (sum > 0) {
            System.out.println(Thread.currentThread().getName()+"卖出：1张，剩余"+--sum);
        }

    }
}
public class test2 {
    public static void main(String[] args) {
        Ticket ticket=new Ticket();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<40;i++){

                    ticket.saleTicket();
                }

            }
        },"A").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<40;i++){

                    ticket.saleTicket();
                }
            }
        },"B").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<40;i++){

                    ticket.saleTicket();
                }
            }
        },"C").start();
    }
}
